package com.htc.callcare.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

import com.htc.callcare.R;
import com.htc.callcare.activities.RecordActivity;
import com.htc.callcare.database.Database;
import com.htc.callcare.model.recordModel;

import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RecordAdapter extends BaseAdapter {

	RecordActivity activity;
	ArrayList<recordModel> records;
	LayoutInflater inflater;
	Hashtable hashtable;
	Database database;
	public boolean longclick;
	String contact_name;

	public RecordAdapter(RecordActivity activity,
			ArrayList<recordModel> records, Hashtable hashtable) {
		super();
		this.activity = activity;
		this.records = records;
		this.hashtable = hashtable;
		inflater = LayoutInflater.from(activity);
		database = new Database(activity);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return records.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		final recordModel model = records.get(position);
		ViewHolder holder;
		 convertView = null;
		holder = new ViewHolder();
		if(records.get(position).getSeparator()==0){
			convertView = inflater.inflate(R.layout.record_row, null);
			convertView.setTag(holder);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.iconCallType);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.note = (TextView) convertView.findViewById(R.id.note);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
			
			if (database.findRecordNew(model.getFileUri()) == 1) {
				holder.title.setTypeface(null, Typeface.BOLD);
			}
			
			if(database.findRecordCheck(model.fileUri)){
				holder.checkBox.setChecked(true);
			}
			else{
				holder.checkBox.setChecked(false);
			}
			
			holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked){
						database.addCheckRecordToDatabase(model);
					}
					else{
						database.deleteCheckRecord(model.fileUri);
					}
				}
			});

			if (model.callType == 1) {
				holder.imageView.setImageDrawable(activity.getResources()
						.getDrawable(R.drawable.icon_income_call));
			} else {
				holder.imageView.setImageDrawable(activity.getResources()
						.getDrawable(R.drawable.icon_outcome_call));
			}

			if (hashtable.containsKey(model.getCallNumber())) {
				contact_name = (String) hashtable.get(model.getCallNumber()) + " ("
						+ model.getCallNumber() + ")";
				
			} else {
				contact_name = model.getCallNumber();
			}
			holder.title.setText(contact_name + "  " + model.getCallTime());
			model.setNote(database.findNote(model.fileUri));
			holder.note.setText(model.getNote());
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (records.size() != 0) {
						database.updateRecordNewIsRead(model.getFileUri());
						activity.showPlayerView();
						activity.setRecordFile(model.getFileUri());
					}
				}
			});
			
			convertView.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					activity.setItemSelect(model, contact_name);
					RecordAdapter.this.notifyDataSetChanged();
					return false;
				}
			});
		}else if(records.get(position).getSeparator()==1){
			convertView = inflater.inflate(R.layout.separator, null);
			convertView.setTag(holder);
			holder.title = (TextView) convertView.findViewById(R.id.textSeparator);
			holder.title.setText(records.get(position).getCallDay());
		}
		

		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
		TextView title;
		TextView note;
		CheckBox checkBox;

	}
	
/*	public ArrayList<recordModel> sortList(ArrayList<recordModel> records) {
		Collections.sort(records);
		return records;
	}*/

	public ArrayList<recordModel> getRecords() {
		return records;
	}

	public void setRecords(ArrayList<recordModel> records) {
		this.records = records;
	}

}
